from todo.models import Todo, importance_choices
from django.http.response import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response


def index(request):
    todos = Todo.objects.all().order_by('importance', 'title')
    choices = importance_choices
    # return render_to_response("todo/index.html", {'todos': todos,
    #                                               'choices': importance_choices,
    #                                               'user': request.user})
    return render_to_response("todo/index.html", locals())


def add(request):
    title = request.POST['title']
    description = request.POST['description']
    importance = request.POST['importance']
    try:
        Todo.objects.create(title=title, description=description, importance=importance)
        return HttpResponse(True)
    except Exception, e:
        return HttpResponse(e)


def remove(request):
    title = request.POST['title']
    try:
        Todo.objects.get(title=title).delete()
        return HttpResponse(True)
    except Exception, e:
        return HttpResponse(e)
